#include "date.h"
#include "widget.h"
#include <QDebug>

void Widget::updateParkingStatus(int no, const QString& plate)
{
    for (auto& spot : m_parkingSpots)
    {
        if (spot->getNo() == no) // 注意这里直接访问了No，实际中应使用一个getter函数或确保No的访问性
        {
            spot->setLicensePlateNumber(plate);
            spot->setOccupied(OCCUPIED);
            qDebug() << "Car parked in spot with No:" << no << "with license plate:" << plate;
            // 更新UI或其他需要的操作
            updateButtonColorForSpot(spot);
            updateWidgetColorForSpot(spot);
            break;
        }
    }
}

void Widget::updateParkingStatus(int no, const QString& license_plate_number, const QString& last_entry_time, const QString& time, int expense)
{
    for (auto& spot : m_parkingSpots)
    {
        if (spot->getNo() == no)
        {
            spot->setLicensePlateNumber(license_plate_number);
            spot->setOccupied(OCCUPIED);
            spot->setLastEntryTime(last_entry_time);
            spot->setCurrentTime(time);
            // 假设ParkingDate类中有一个设置费用的函数
            spot->setExpense(expense);
            qDebug() << "Car parked in spot with No:" << no << "with license plate:" << license_plate_number
                     << "at:" << last_entry_time << "with expense:" << expense;
            updateButtonColorForSpot(spot);
            updateWidgetColorForSpot(spot);
            break;
        }
    }
}

void Widget::setupConnections() {
    // ...

    connect(&workerThread, &WorkerThread::dataReceived, this, &Widget::on_dataReceived);

    // ...
}

void Widget::on_dataReceived(const QByteArray &data)
{
    QJsonDocument doc = QJsonDocument::fromJson(data);
    if (!doc.isNull() && doc.isArray()) {
        QJsonArray jsonArray = doc.array();
        for (const QJsonValue &value : jsonArray) {
            if (value.isObject()) {
                QJsonObject jsonObject = value.toObject();

                // 获取JSON对象中的数据
                int no = jsonObject["No"].toInt();
                QString licensePlate = jsonObject["license_plate_number"].toString();
                QString lastEntryTime = jsonObject["last_entry_time"].toString();
                QString currentTime = jsonObject["time"].toString();
                int expense = jsonObject["expense"].toInt();

                bool isOccupied = !licensePlate.isEmpty(); // 如果车牌不为空，车位被占用

                // 更新ParkingDate对象
                for (auto& spot : m_parkingSpots) {
                    if (spot->getNo() == no) { // 使用"No"来匹配ParkingDate对象
                        spot->setOccupied(isOccupied);
                        spot->setLicensePlateNumber(licensePlate);
                        spot->setLastEntryTime(lastEntryTime);
                        spot->setCurrentTime(currentTime);
                        spot->setExpense(expense);
                        updateButtonColorForSpot(spot);
                        updateWidgetColorForSpot(spot);
                        break;
                    }
                }
            }
        }
    }
}
